<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客详情页</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_detail.css">

    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="editor.md/editormd.js"></script>
</head>
<body>
<div class="nav">
    <img src="image/logo2.jpg" alt="">
    <div class="title">我的博客系统</div>
    <div class="spacer"></div>
    <a href="blog_list.html">主页</a>
    <a href="blog_edit.html">写博客</a>
    <a href="logout">注销</a>
</div>
<div class="container">
    <div class="container-left">
        <div class="card">
            <img src="image/logo2.jpg" alt="">
            <h3></h3>
            <a href="https://www.github.com">github 地址</a>
            <div class="counter">
                <span>文章</span>
                <span>分类</span>
            </div>
            <div class="counter">
                <span>2</span>
                <span>1</span>
            </div>
        </div>
    </div>

    <div class="container-right">
        <h3></h3>
        <div class="date"></div>
        <div id="content">
        </div>
    </div>
</div>

<script src="js/app.js"></script>
<script>
    function getBlog() {
        $.ajax({
            type: 'get',
            url: 'blog' + location.search,
            success: function(body) {
                let h3 = document.querySelector('.container-right h3');
                h3.innerHTML = body.title;
                let dateDiv = document.querySelector('.container-right .date');
                dateDiv.innerHTML = body.postTime;

                editormd.markdownToHTML('content', {markdown: body.content});
            }
        });
    }

    getBlog();

    getLoginStatus();

    function getAuthor() {
        $.ajax({
            type: 'get',
            url: 'user' + location.search,
            success: function(body) {
                if (body.userId == 0) {
                    alert("当前未找到作者信息!");
                    return;
                }
                let h3 = document.querySelector('.card h3');
                h3.innerHTML = body.username;
            }
        });
    }

    getAuthor();
    $(function () {
        $.ajax({
            type: 'get',
            url: 'user' + location.search,
            success: function (respData) {
                if (respData.code == 0) {
                    let user = respData.data;
                    $('#h_datails_username').html(user.username);
                    let htmlStr = '<a href="javascript:void(0);">删除</a>';
                    let deleteEl = $(htmlStr);
                    $('.opts').append(deleteEl);
                    deleteEl.click(deleteBlog);

                } else {
                    alter(respData.message);
                }
            },
            error: function () {
                console.log('访问出现错误');
            },
            statusCode: {
                403: function () {
                    location.assign('blog_login.html');
                }
            }
        });

        function deleteBlog() {
            if (!confirm('是否删除？')) {
                return;
            }
            $.ajax({
                type: 'delete',
                url: 'blog' + location.search,
                success: function (respData) {
                    if (respData.code == 0) {
                        location.assign('blog_list.html')

                    } else {
                        alter(respData.message);
                    }
                },
                error: function () {
                    console.log('访问出现错误');
                },
                statusCode: {
                    403: function () {
                        location.assign('blog_login.html');
                    }
                }
            });

        }
    })


</script>
</body>
</html>